<?php

class Users_model extends CI_Model{

	function __construct() 
	{
		parent::__construct();
	}
	
	function verifyUser($u,$pw)
	{
		$this->db->select('id');
		$this->db->where('username',$u);
		$this->db->where('password', md5($pw));
		$this->db->where('status', 'active');
		$this->db->limit(1);
		$Q = $this->db->get('users');
		if ($Q->num_rows() > 0)
		{
			$row = $Q->row_array();
			return $row;
		}
		else
		{
			return array();
		}
	}
	
	
	function getUser($id)
	{
		$data = array();
		$this->db->where('id',$id);
		$this->db->limit(1);
		$Q = $this->db->get('users');
		if ($Q->num_rows() > 0)
	  	{
	  		$data = $Q->row_array();
		}
		$Q->free_result();
		return $data;
	}
	
	function getAllUsers()
	{
     	$data = array();
     	$Q = $this->db->get('users');
     	if ($Q->num_rows() > 0)
		{
       		foreach ($Q->result_array() as $row)
			{
         	$data[] = $row;
       		}
     	}
     	$Q->free_result();    
     	return $data; 	
	}
	
	function addUser(){
      $data = array('username' => $_POST['username'],
                    'email' => $_POST['email'],
                    'status' => $_POST['status'],
                    'password' => md5($_POST['password'])
                    );
	
	  $this->db->insert('users',$data);
	
	}
	
	function updateUser(){
      $data = array('username' => $_POST['username'],
                    'email' => $_POST['email'],
                    'status' => $_POST['status'],
                    'password' => md5($_POST['password'])
                    );
	  $this->db->where('id',$_POST['id']);
	  $this->db->update('users',$data);	
	
	}
	
	
	function deleteUser($id){
 	 $data = array('status' => 'inactive');
 	 $this->db->where('id', $id);
	 $this->db->update('users', $data);
	
	}

}//end class
?>